$(document).ready(function(){

	
	//上传图片
	new AjaxUpload('#upload-factory-photo', {
		action: basePath+'/factoryinspection/uploadFile.do', 
		name: 'picFile',
		responseType: 'json',
		onSubmit : function(file , ext){
//			if (ext && /^(xls|csv|xlsx)$/.test(ext.toLowerCase())){
//				this.setData({
//					'picName': file
//				});
//			} else {
//				alert("请上传格式为 xls|csv|xlsx 的文件！");
//				return false;				
//			}
			

			
			
		},
		onComplete : function(file,response){
			if(response.error) {
				alert(response.error);
				return;
			}else
			{
				
									
				response.data.forEach(function(log){
					
					
					addValToInput($('#factory-photoes'),log);
					
					renderPhotoes();
					
				})
				
				
			}
			
			
			
			
		}		
	});
	
	
	
	//上传图片
	new AjaxUpload('#upload-license', {
		action: basePath+'/factoryinspection/uploadFile.do', 
		name: 'picFile',
		responseType: 'json',
		onSubmit : function(file , ext){
//			if (ext && /^(xls|csv|xlsx)$/.test(ext.toLowerCase())){
//				this.setData({
//					'picName': file
//				});
//			} else {
//				alert("请上传格式为 xls|csv|xlsx 的文件！");
//				return false;				
//			}
			

			
			
		},
		onComplete : function(file,response){
			if(response.error) {
				alert(response.error);
				return;
			}else
			{
				
									
				response.data.forEach(function(log){
					
					
					//$('#license-photo').val(log);
					addValToInput($('#license-photo'),log);
					
					renderPhotoes();
					
				})
				
				
			}
			
			
			
			
		}		
	});
	
	
	renderPhotoes();
	
	$('ul').on('click','.f-img',function(){
				
		if($(this).height()===60)
		{
			
			$(this).css({width:'100%',height:"auto"});
			
			$(this).next().show();
		
		}else{
			
			$(this).css({width:'auto',height:'60px'});
			
			$(this).next().hide();
		
		}
		
	})
	
	$('#factory-photoes-container').on('click','a',function(){
		
		
		var c=confirm("确实删除此工厂图片吗？");
		
		if (c==false) return;
		
		removeValFromInput($('#factory-photoes'),$(this).attr('data'));
		
		$(this).parent().parent().parent().remove();
		
		
	})
	
		$('#license-container').on('click','a',function(){
		
		
		var c=confirm("确实删除此营业执照图片吗？");
		
		if (c==false) return;
		
		removeValFromInput($('#license-photo'),$(this).attr('data'));
		
		$(this).parent().parent().parent().remove();
		
		
	})
	
})

function renderPhotoes()
{	
	var parr=$('#factory-photoes').val().split(',');
	
	$('#factory-photoes-container').empty();
	
	for(var i=0;i<parr.length;i++)
	{
		if(parr[i]=='') continue;
		
		var li='<li><div style="position:relative;"><img class="f-img" style="height:60px;cursor:pointer;" src="http://attach.easylinking.net'+parr[i]+'">'+
		
		'<span class="badge" style="display:none;position:absolute;right:5px;top:5px;"><a href="javascript:void(0)" data="'+parr[i]+'" role="button"><span class="" style="color:red;">X</span></a></span>'+
		'</div></li>';
		
		$('#factory-photoes-container').append(li);
	}
	
	
	var larr=$('#license-photo').val().split(',');
	
	$('#license-container').empty();
	
	for(var i=0;i<larr.length;i++)
	{
		if(larr[i]=='') continue;
		
		var li='<li><div style="position:relative;"><img class="f-img" style="height:60px;cursor:pointer;" src="http://attach.easylinking.net'+larr[i]+'">'+
		'<span class="badge" style="display:none;position:absolute;right:5px;top:5px;"><a href="javascript:void(0)" data="'+larr[i]+'" role="button"><span class="" style="color:red;">X</span></a></span>'+
		'</div></li>';
		$('#license-container').append(li);
	}	
	
		
}


function renderFactoryPhotoesForObj(parr)
{	
	$('#factory-photoes').val(parr);
	
	var parr=parr.split(',');
	
	$('#factory-photoes-container').empty();
	
	for(var i=0;i<parr.length;i++)
	{
		if(parr[i]=='') continue;
		
		var li='<li><div style="position:relative;"><img class="f-img" style="height:60px;cursor:pointer;" src="http://attach.easylinking.net'+parr[i]+'">'+
		
		'<span class="badge" style="display:none;position:absolute;right:5px;top:5px;"><a href="javascript:void(0)" data="'+parr[i]+'" role="button"><span class="" style="color:red;">X</span></a></span>'+
		'</div></li>';
		
		$('#factory-photoes-container').append(li);
	}
	
}

function renderLicensePhotoesForObj(larr){
	
	$('#license-photo').val(larr);
	
	var larr=larr.split(',');
	
	$('#license-container').empty();
	
	for(var i=0;i<larr.length;i++)
	{
		if(larr[i]=='') continue;
		
		var li='<li><div style="position:relative;"><img class="f-img" style="height:60px;cursor:pointer;" src="http://attach.easylinking.net'+larr[i]+'">'+
		'<span class="badge" style="display:none;position:absolute;right:5px;top:5px;"><a href="javascript:void(0)" data="'+larr[i]+'" role="button"><span class="" style="color:red;">X</span></a></span>'+
		'</div></li>';
		$('#license-container').append(li);
	}	
	
}



function addValToInput(obj,val)
{
	var ids = [];

    if ($(obj).val() != '') {
        ids = $(obj).val().split(',');
    }

    ids.push(val);
    
    ids=ids.delRepeat();

    $(obj).val(ids.join(','));

}

function removeValFromInput(obj,val)
{
	
	var ids = [];

    if ($(obj).val() != '') {
     
    	ids = $(obj).val().split(',');
    
    	ids = ids.delId(val);

    	$(obj).val(ids.join(','));
    }
}

//删除重复
Array.prototype.delRepeat = function () {
    var newArray = [];
    var provisionalTable = {};
    for (var i = 0, item; (item = this[i]) != null; i++) {
        if (!provisionalTable[item] && item!="") {
            newArray.push(item);
            provisionalTable[item] = true;
        }
    }
    return newArray;
}

//删除其中一个
Array.prototype.delId = function (id) {
       

    for (var i = 0; i < this.length; i++) {
        if (this[i] == id) {
            this.splice(i, 1);
            break;
        }
    }

    return this;
}
